import { fromJS } from 'immutable';

import * as actionTypes from './actionTypes';
import { getRedirectPath } from '../../util';

const defaultState = fromJS({
    redirectTo: '',
    user: '',
    type: '',
    msg: ''
})

export default (state = defaultState, action) => {
    switch(action.type) {
        case actionTypes.ERROR_MSG:
            return state.set('msg', action.msg);
        case actionTypes.AUTH_SUCCESS:
            return state.merge({
                user: action.user,
                redirectTo: getRedirectPath(action.user)
            });
        case actionTypes.LOGIN_DATA:
            return state.merge({
                user: action.user,
                redirectTo: getRedirectPath(action.user)
            });
        case actionTypes.LOGOUT:
            return state.merge({
                user: '',
                redirectTo: ''
            });
        default:
            return state;
    }
}